﻿using ComLib.ConfigService;
using LogLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ComLib
{
    public class OutputDirectory : IniConfigBase
    {
        private string _rootDir = "D:\\PersonData";
        public string RootDir
        {
            get { return _rootDir; }
            set { _rootDir = value; }
        }

        private DirectoryInfo _todayDirInfo = null;

        /// <summary>
        /// 创建当天输出目录
        /// </summary>
        private void CreatTodaySaveDirectory()
        {
            if (!Directory.Exists(RootDir))
            {
                Directory.CreateDirectory(RootDir);
                Log.WriteAsync("创建新目录:" + RootDir);
            }
            string directory = string.Format(@"{0}\{1}\{2}",
                   RootDir,
                   System.DateTime.Now.ToString("yyyyMM"),
                   System.DateTime.Now.ToString("dd"));
            if (_todayDirInfo == null)
            {
                _todayDirInfo = new DirectoryInfo(directory);
                if (!_todayDirInfo.Exists)
                {
                    _todayDirInfo.Create();
                    Log.WriteAsync("创建新目录:" + directory);
                    return;
                }
            }
            if (!_todayDirInfo.CreationTime.Date.Equals(DateTime.Now.Date))
            {
                _todayDirInfo = new DirectoryInfo(directory);
                if (!_todayDirInfo.Exists)
                {
                    _todayDirInfo.Create();
                    Log.WriteAsync("创建新目录:" + directory);
                    return;
                }
            }
            if (!_todayDirInfo.Exists)
            {
                _todayDirInfo.Create();
                Log.WriteAsync("创建新目录:" + directory);
                return;
            }
        }

        /// <summary>
        /// 获取个人数据保存目录
        /// </summary>
        /// <returns></returns>
        public string this[string sfzh]
        {
            get 
            {
                CreatTodaySaveDirectory();
                if (string.IsNullOrEmpty(sfzh))
                {
                    return _todayDirInfo.FullName;
                }
                string personDir = Path.Combine(_todayDirInfo.FullName, sfzh);
                if (!Directory.Exists(personDir))
                {
                    Directory.CreateDirectory(personDir);
                }
                return personDir;
            }           
        }
   
        public override bool Save()
        {
            base.Save();
            SaveField("RootDir", _rootDir);
            return true;
        }

        public override bool Load()
        {
            base.Load();
            try
            {
                RootDir = LoadStringValue("RootDir");
            }
            catch (Exception ex)
            {
                ErrorMessage = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("数据保存目录:");
            builder.Append("[个人数据保存根目录]: " + RootDir);
            return builder.ToString();
        }
    }
}
